package com.dtwave.wechat.service.provider.core.aop;

import com.dtwave.wolf.utils.TimeProfileUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * ClassName: ExecutionTimeLog <br/>
 * Function: 计算执行时间日志aop <br/>
 * @author: liuwenxiang.<br>
 * 创建时间 : 2018-12-17 下午7:01.<br>
 */
@Component
@Aspect
public class ExecutionTimeLogAop {

    /**
     * Service aspect.
     */
    @Pointcut("execution (public * com.dtwave.wechat.service.provider.third.impl.*.*(..)) " +
            "|| execution (public * com.dtwave.wechat.service.provider.service.impl.*.*(..))")
    public void serviceAspect() {
        //
    }

    /**
     * Service around.
     *
     * @param joinPoint
     *         the join point
     *
     * @return the object
     *
     * @throws Throwable
     *         the throwable
     */
    @Around("serviceAspect()")
    public Object serviceAround(ProceedingJoinPoint joinPoint) throws Throwable {
        try {
            Signature signature = joinPoint.getSignature();
            String fullName = signature.getDeclaringTypeName();
            TimeProfileUtils.enter(fullName.substring(fullName.lastIndexOf('.') + 1) + " " + signature.getName());
            Object ret = joinPoint.proceed();

            return ret;
        } finally {
            TimeProfileUtils.release();
        }
    }
}
